<?php

require_once 'connect.php';

$state = $_GET['state'];
if ($state == "initial") {
    echo "{list:{title:'Presidential Poll Survey',items: [{id:'vote', label:'Vote'},{id:'view', label:'View'}]}}";
} else if ($state == "select") {
    $sid = $_GET['sid'];
    if ($sid == "vote") {
        echo "{form:{
            title:'Fill this form',
            form_id:'vd',
            items:[
                {textfield:{id:'cou', label:'Your County'}},
                {textfield:{id:'age',label:'Your Age'}},
               {checkbox:{id:'reg', label:'Check if registered voter (at only KSh50)'}},
                {radiobutton:{id:'occ',label:'Occupation',choices:[{choice:'Employed'},{choice:'Unemployed'},{choice:'Student'}]}}
                ]
          }
 }";
    } else if ($sid == "view") {
        require_once 'results.php';
    } else if ($sid == "kalonzo" || $sid == "karua" || $sid == "ruto" || $sid == "raila" || $sid == "undecided") {

        $query = "SELECT * FROM polls_survey order by id desc limit 1";
        $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
        if (mysql_num_rows($result) > 0) {
            $row = mysql_fetch_array($result);
            $id = $row['id'];

            $query = "UPDATE polls_survey SET candidate='$sid' WHERE id='$id'";
            $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());

            require_once 'results.php';
        } else {
            echo "No way u can vote without first submiting ya info.";
        }
    } else {
        echo "{form:{
                title:'Third party Error',
                form_id:'hj',
                items:[
                        {label:
                            {text:'Invalid state $sid'}
                        }
                    ]
                }
          }";
    }
//    include 'lists.php';
} else if ($state == "submit") {
    $age = $_GET['age'];
    $cou = $_GET['cou'];
    $occ = $_GET['occ'];
    $reg = $_GET['reg'];
    $query = "INSERT INTO polls_survey SET age='$age', county='$cou',nationality='$occ' ";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    if ($reg == "true") {
        echo "{pay:{amount:50}}";
    } else {
        echo "{list:{title:'Select a candidate',items: [{id:'kalonzo', label:'Kalonzo Musyoka'},{id:'karua', label:'Martha Karua'},{id:'ruto', label:'William Samoei'},{id:'raila', label:'Raila Odinga'},{id:'undecided', label:'Undecided'}]}}";
    }
} else if ($state == "paid_success") {
    $names = $_GET['names'];
    $code = $_GET['code'];
    $amount = $_GET['amount'];
    $phoneNo = $_GET['phoneNo'];
    echo "{form:{
                title:'Payment successful',
                form_id:'sa',
                items:[
                        {label:
                            {text:'We have recived an amount of Ksh$amount from $names $phoneNo on Trans code $code'}
                        }
                    ]
                }
          }";
} else if ($state == "paid_pending") {
    echo "{form:{
                title:'Payment failed',
                form_id:'x',
                items:[
                        {label:
                            {text:'The code you entered was not found, please try again after some time'}
                        }
                    ]
                }
          }";
} else if ($state == "paid_used") {
    echo "{form:{
                title:'Payment failed',
                form_id:'y',
                items:[
                        {label:
                            {text:'The code you entered was used before.'}
                        }
                    ]
                }
          }";
} else {
    echo "{form:{
                title:'Third party Error',
                form_id:'error',
                items:[
                        {label:
                            {text:'Invalid state $state, This app support Lists and forms only.'}
                        }
                    ]
                }
          }";
}